package com.jack.bookshop.servlet;

import com.jack.bookshop.util.DatabaseUtil;
import com.jack.bookshop.vo.OrderBookVo;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "PayServlet", value = "/PayServlet")
public class PayServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Connection conn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        List<OrderBookVo> list = new ArrayList<>();
        try {
            conn = DatabaseUtil.getConnection();
            String sql = "select * from cart where checked = 1";


            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            while (rs.next()){
                OrderBookVo book = new OrderBookVo();
                book.setBook_id(rs.getInt("book_id"));
                book.setBook_image_url(rs.getString("book_image_url"));
                book.setBook_title(rs.getString("book_title"));
                book.setBook_price(rs.getBigDecimal("book_price"));
                book.setBook_discount(rs.getFloat("book_discount"));
                book.setQuantity(rs.getInt("quantity"));
                book.setId(rs.getInt("id"));
                list.add(book);
            }
            request.getSession().setAttribute("bookorder",list);
            response.sendRedirect("order.jsp");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
